/**
 * @author by zyl
 * @date 2020/3/28.
 */
public class XorUtils {

    public static String xor(String h,String key) {
        int l1 = h.length();
        int l2 = key.length();
        String r = "";
        for (int i = 0; i < l1; i = i + l2) {
            int j = i + l2;
            if (j >= h.length())
                j = h.length();
            r += eh(h.substring(i, j), key);
        }
        String s =r;
        return s;
    }

    public static String eh(String s1, String s2) {
        byte b1[] = s1.getBytes();
        byte b2[] = s2.getBytes();
        byte lb[], sb[];
        lb = b1.length >= b2.length ? b1 : b2;
        sb = b1.length >= b2.length ? b2 : b1;
        byte rb[] = new byte[sb.length];
        int i = 0;
        for (; i < sb.length; i++) {
            rb[i] = (byte) (sb[i] ^ lb[i]);
        }
        return new String(rb);
    }
}